﻿// $Id: CrudProviderHelper.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#pragma warning disable 1587
/** \file
 * \brief Definition of \c Salamanca.DataAccess.Web.CrudProviderHelper.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */
#pragma warning restore 1587

#if (!NET_CF)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Salamanca.DataAccess.Web
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Provides methods for web services oriented <include file="../doc/Global.doc.xml" path="doc/template[@name='DataMapper']/*" />s.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public static class CrudProviderHelper
    {

        /// <summary>Gets the array of <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> corresponding to
        /// the list of <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> returned by the specified <see cref="Finder{TDomainModel}" />.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <typeparam name="TDomainModelDataTransferObject">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> type.</typeparam>
        /// <param name="find">A delegate that returns a list of <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />s.</param>
        /// <param name="dataMappers">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataMapper']/*" />s collection.</param>
        /// <returns>The array of <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" />.</returns>
        public static List<TDomainModelDataTransferObject> DomainModelFind<TDomainModel, TDomainModelDataTransferObject>(Finder<TDomainModel> find, Collections.DataMapperCollection dataMappers)
            where TDomainModel:
                IDomainModel
            where TDomainModelDataTransferObject:
                IDomainModelDataTransferObject
        {
            ICollection<TDomainModel> res=find();
            if (res!=null)
                return new List<TDomainModel>(res).ConvertAll<TDomainModelDataTransferObject>(
                    delegate(TDomainModel m) {
                        return (TDomainModelDataTransferObject)dataMappers[typeof(TDomainModel)].GetData(m, true);
                    }
                );
            else
                return null;
        }



        /// <summary>Deletes the <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> corresponding to the specified 
        /// <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> with the specified <see cref="Deleter{TDomainModel}" />.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <typeparam name="TDomainModelDataTransferObject">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> type.</typeparam>
        /// <param name="data">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" />.</param>
        /// <param name="force">Indicates wheteher the deletion should be forced.</param>
        /// <param name="delete">A delegate that deletes a <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</param>
        /// <param name="dataMappers">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataMapper']/*" />s collection.</param>
        /// <returns>The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> of the deleted <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</returns>
        public static TDomainModelDataTransferObject DomainModelDelete<TDomainModel, TDomainModelDataTransferObject>(TDomainModelDataTransferObject data, bool force, Deleter<TDomainModel> delete, Collections.DataMapperCollection dataMappers)
            where TDomainModel:
                IDomainModel
            where TDomainModelDataTransferObject:
                IDomainModelDataTransferObject
        {
            TDomainModel m=(TDomainModel)dataMappers[typeof(TDomainModel)].GetDomainModel(data, dataMappers);
            delete(m, force);

            return (TDomainModelDataTransferObject)dataMappers[typeof(TDomainModel)].GetData(m, true);
        }



        /// <summary>Saves the <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> corresponding to the specified 
        /// <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> with the specified <see cref="Saver{TDomainModel}" />.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <typeparam name="TDomainModelDataTransferObject">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> type.</typeparam>
        /// <param name="data">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" />.</param>
        /// <param name="force">Indicates wheteher the save should be forced.</param>
        /// <param name="save">A delegate that saves a <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</param>
        /// <param name="dataMappers">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataMapper']/*" />s collection.</param>
        /// <returns>The <include file="../doc/Global.doc.xml" path="doc/template[@name='DataTransferObject']/*" /> of the saved <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</returns>
        public static TDomainModelDataTransferObject DomainModelSave<TDomainModel, TDomainModelDataTransferObject>(TDomainModelDataTransferObject data, bool force, Saver<TDomainModel> save, Collections.DataMapperCollection dataMappers)
            where TDomainModel:
                IDomainModel
            where TDomainModelDataTransferObject:
                IDomainModelDataTransferObject
        {
            TDomainModel m=(TDomainModel)dataMappers[typeof(TDomainModel)].GetDomainModel(data, dataMappers);
            save(m, force);

            return (TDomainModelDataTransferObject)dataMappers[typeof(TDomainModel)].GetData(m, true);
        }



        /// <summary>Delegate that returns a list of <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />s.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <returns>The list of <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />s.</returns>
        public delegate IList<TDomainModel> Finder<TDomainModel>()
            where TDomainModel:
                IDomainModel;

        /// <summary>Delegate that deletes a <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <param name="m">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> to be deleted.</param>
        /// <param name="force">Indicates whether the deletion should be forced.</param>
        public delegate void Deleter<TDomainModel>(TDomainModel m, bool force)
            where TDomainModel:
                IDomainModel;

        /// <summary>Delegate that saves a <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" />.</summary>
        /// <typeparam name="TDomainModel">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> type.</typeparam>
        /// <param name="m">The <include file="../doc/Global.doc.xml" path="doc/template[@name='DomainModel']/*" /> to be saved.</param>
        /// <param name="force">Indicates whether the save should be forced.</param>
        public delegate void Saver<TDomainModel>(TDomainModel m, bool force)
            where TDomainModel:
                IDomainModel;
    }

}
#endif
